<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="query.keyword" placeholder="名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table
      :data="pageInfo.pageList"
      highlight-current-row
      v-loading="listLoading"
      @selection-change="selsChange"
      style="width: 100%"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column type="index" width="60"> </el-table-column>
      <el-table-column prop="key" label="key" width="180" sortable>
      </el-table-column>
      <el-table-column prop="value" label="value" width="180" sortable>
      </el-table-column>
      <el-table-column prop="intro" label="介绍" width="180" sortable>
      </el-table-column>
      <el-table-column label="操作">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            size="small"
            @click="handleDel(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button
        type="danger"
        @click="batchRemove"
        :disabled="this.sels.length === 0"
        >批量删除</el-button
      >
      <el-pagination
        layout="prev, pager, next"
        @current-change="handleCurrentChange"
        :page-size="query.pageSize"
        :current-page="query.currentPage"
        :total="pageInfo.count"
        style="float: right"
      >
      </el-pagination>
    </el-col>

    <!--新增/编辑界面-->
    <el-dialog
      title="新增/编辑"
      :visible.sync="addFormVisible"
      :close-on-click-modal="false"
    >
      <el-form
        :model="addForm"
        label-width="80px"
        :rules="addFormRules"
        ref="addForm"
      >
        <el-form-item label="key">
          <el-input v-model="addForm.key" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="value">
          <el-input v-model="addForm.value" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="介绍">
          <el-input type="textarea" v-model="addForm.intro" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addFormVisible = false">取消</el-button>
        <el-button
          type="primary"
          @click.native="addSubmit"
          :loading="addLoading"
          >提交</el-button
        >
      </div>
    </el-dialog>
  </section>
</template>

<script>
import util from '../../common/js/util'
//import NProgress from 'nprogress'
import {
  getUserListPage,
  removeUser,
  batchRemoveUser,
  addUser,
} from '../../api/api'

export default {
  data() {
    return {
      filters: {
        name: '',
      },
      users: [],
      total: 0,
      page: 1,
      listLoading: false,
      sels: [], //列表选中列

      addFormVisible: false, //新增界面是否显示
      addLoading: false,
      addFormRules: {
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        sn: [{ required: true, message: '标识必填', trigger: 'blur' }],
      },
      //新增界面数据
      addForm: {
        id: null,
        key: '',
        value: '',
        intro: ''
      },
      query: {
        currentPage: 1,
        pageSize: 5,
        keyword: '',
      },
      pageInfo: {
        count: 0,
        pageList: [],
      },
    }
  },
  methods: {
    search() {
      this.query.currentPage = 1
      this.getConfigs()
    },
    handleSizeChange(val) {
      console.log('页', val)
    },
    //性别显示转换
    formatSex: function (row, column) {
      return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知'
    },
    handleCurrentChange(val) {
      this.query.currentPage = val
      this.getConfigs()
    },
    //获取用户列表
    getConfigs() {
      this.listLoading = true
      this.$http.post('/config', this.query).then((res) => {
        res = res.data
        if (res.success) {
          this.listLoading = false
          this.pageInfo = res.data
        } else {
          this.$message.error(res.message)
        }
      })
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning',
      })
        .then(() => {
          this.$http.delete('/config/' + row.id).then((res) => {
            res = res.data
            if (res.success) {
              this.$message.success('删除成功')
              this.search()
            } else {
              this.$message.error(res.message)
            }
          })
        })
        .catch(() => {})
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.addFormVisible = true
      console.log('编辑数据', row)
      this.addForm = Object.assign({}, row)
    },
    //显示新增界面
    handleAdd: function () {
      this.addFormVisible = true
      this.addForm = {
        id: null,
        key: '',
        value: '',
        intro: ''
      }
    },
    //新增
    addSubmit: function () {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.addLoading = true
            this.$http.put('/config', this.addForm).then((res) => {
              res = res.data
              if (res.success) {
                this.$message.success('保存成功')
                // 关闭加载
                this.addLoading = false
                // 关闭弹窗
                this.addFormVisible = false
                this.getConfigs()
              } else {
                this.$message.error(res.message)
              }
            })
          })
        }
      })
    },
    selsChange: function (sels) {
      this.sels = sels
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map((item) => item.id)
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning',
      })
        .then(() => {
          this.$http.patch('/config', ids).then((res) => {
            res = res.data
            if (res.success) {
              this.$message.success('批量删除成功')
              this.search()
            } else {
              this.$message.error(res.message)
            }
          })
        })
        .catch(() => {})
    },
  },
  mounted() {
    this.getConfigs()
  },
}
</script>

<style scoped>
.line-check {
  display: inline-block;
  margin-left: 20px;
  margin-bottom: 20px;
}
</style>
